﻿using DeviceMaintainGIS.Entity;
using DeviceMaintainGIS.Model;
using DeviceMaintainGIS.Model.LocaInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace DeviceMaintainGIS.BLL.Loca
{
    /// <summary>
    /// 基站操作
    /// </summary>
    public class LocaService
    {
        /// <summary>
        /// 通过ID获取基站信息
        /// </summary>
        /// <param name="LocaId"></param>
        /// <returns></returns>
        public ReturnVM<LocaInfoModel> GetLocaListByID(string LocaId)
        {
            ReturnVM<LocaInfoModel> model = new ReturnVM<LocaInfoModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.LocaInfo_Tab.Where(i => i.LocaId == LocaId)
                           select new LocaInfoModel
                           {
                               LocaId = a.LocaId,
                               LocaName = a.LocaName,
                               LocaTypeId = a.LocaTypeId,
                               LocaHigh = a.LocaHigh,
                               Describe = a.Describe,
                               Link = a.Link,
                               PowerMode = a.PowerMode,
                               District = a.District,
                               Town = a.Town,
                               Longitude = a.Longitude,
                               Latitude = a.Latitude,
                               Altitude = a.Altitude,
                               NaviLongitude = a.NaviLongitude,
                               NaviLatitude = a.NaviLatitude,
                               StopLongitude = a.StopLongitude,
                               StopLatitude = a.StopLatitude,
                               Village = a.Village,
                               Remark = a.Remark,
                               AddTime = a.AddTime,

                           };
                model.obj = linq.FirstOrDefault();
                List<LocaManager_Tab> LocaManagerLinq = Entities.LocaManager_Tab.Where(i => i.LocaId == LocaId).ToList();
                if (LocaManagerLinq.Count > 0)
                {
                    JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
                    model.obj.LocaManagerTablJson = jsonSerialize.Serialize(LocaManagerLinq);
                }
            }
            return model;
        }
        /// <summary>
        /// 通过条件查询基站信息列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ReturnListVM<LocaInfoModel> GetLocaList(LocaRequest request)
        {
            ReturnListVM<LocaInfoModel> list = new ReturnListVM<LocaInfoModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.LocaInfo_Tab
                           join f in Entities.LocaType_Tab on a.LocaTypeId equals f.LocaTypeId
                           into tmpLocaType
                           let k = tmpLocaType.FirstOrDefault()
                           select new LocaInfoModel
                           {
                               LocaId = a.LocaId,
                               LocaName = a.LocaName,
                               LocaTypeId = a.LocaTypeId,
                               LocaTypeName = k == null ? "" : k.LocaTyp,
                               LocaHigh = a.LocaHigh,
                               Describe = a.Describe,
                               Link = a.Link,
                               PowerMode = a.PowerMode,
                               District = a.District,
                               Town = a.Town,
                               Longitude = a.Longitude,
                               Latitude = a.Latitude,
                               Altitude = a.Altitude,
                               NaviLongitude = a.NaviLongitude,
                               NaviLatitude = a.NaviLatitude,
                               StopLongitude = a.StopLongitude,
                               StopLatitude = a.StopLatitude,
                               Village = a.Village,
                               Remark = a.Remark,
                               AddTime = a.AddTime
                           };
                //模糊查询
                if (!string.IsNullOrEmpty(request.Indistinct))
                {
                    linq = linq.Where(i => i.LocaName.Contains(request.Indistinct) || i.District.Contains(request.Indistinct) || i.Town.Contains(request.Indistinct));
                }
                ////项目名称
                //if (!string.IsNullOrEmpty(request.LocaName))
                //{
                //    linq = linq.Where(i => i.LocaName.Contains(request.LocaName));
                //}
                ////区县
                //if (!string.IsNullOrEmpty(request.District))
                //{
                //    linq = linq.Where(i => i.District.Contains(request.District));
                //}
                ////乡镇
                //if (!string.IsNullOrEmpty(request.Town))
                //{
                //    linq = linq.Where(i => i.Town.Contains(request.Town));
                //}

                list.TotalCount = linq.Count();
                List<LocaInfoModel> LocaInfolist = linq.OrderBy(i => i.LocaId).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();
                foreach (LocaInfoModel lim in LocaInfolist)
                {
                    List<LocaManager_Tab> LocaManagerLinq = Entities.LocaManager_Tab.Where(i => i.LocaId == lim.LocaId).ToList();
                    if (LocaManagerLinq.Count > 0)
                    {
                        JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
                        lim.LocaManagerTablJson = jsonSerialize.Serialize(LocaManagerLinq);
                    }
                }

                foreach (LocaInfoModel lim in LocaInfolist)
                {
                    List<LocaManager_Tab> LocaManagerLinq = Entities.LocaManager_Tab.Where(i => i.LocaId == lim.LocaId).ToList();
                    if (LocaManagerLinq.Count > 0)
                    {
                        JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
                        lim.LocaManagerTablJson = jsonSerialize.Serialize(LocaManagerLinq);
                    }

                    int Documentint = Entities.DocumentRelation_Tab.Where(i => i.Type == 2 && i.LPDId == lim.LocaId).Count();
                    if (Documentint > 0)
                    {
                        lim.IsDocument = true;
                    }
                    else
                    {
                        lim.IsDocument = false;
                    }

                    int Proint = Entities.DevProRelation_Tab.Where(i => i.LocaId == lim.LocaId && i.CodeId == "1").Count();
                    if (Proint > 0)
                    {
                        lim.IsPro = true;
                    }
                    else
                    {
                        lim.IsPro = false;
                    }

                    //int Devint = Entities.DevProRelation_Tab.Where(i => i.LocaId == lim.LocaId).Count();
                    //if (Devint > 0)
                    //{
                    //    lim.IsDev = true;
                    //}
                    //else
                    //{
                    //    lim.IsDev = false;
                    //}
                    lim.IsDev = false;
                    List<string> DevIDs = Entities.DevProRelation_Tab.Where(i => i.LocaId == lim.LocaId).Select(i => i.DeviceId).ToList();
                    foreach (string devid in DevIDs)
                    {
                        DevInfo_Tab di = Entities.DevInfo_Tab.Where(i => i.DeviceId == devid && i.State == 1).FirstOrDefault();
                        if (di != null)
                        {

                            lim.IsDev = true;
                        }
                    }
                }
                list.List = LocaInfolist;
                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;
            }
            return list;
        }
        /// <summary>
        /// 通过设备 项目查询基站信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ReturnListVM<LocaInfoExtendModel> GetLocaExtendList(LocaRequest request)
        {
            ReturnListVM<LocaInfoExtendModel> list = new ReturnListVM<LocaInfoExtendModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.DevProRelation_Tab.Where(i => i.LocaId != null)
                           join f in Entities.LocaInfo_Tab on a.LocaId equals f.LocaId
                           into tmpLocaInfo
                           let LocaInfoTab = tmpLocaInfo.FirstOrDefault()

                           join l in Entities.LocaType_Tab on LocaInfoTab.LocaTypeId equals l.LocaTypeId
                           into tmpLocaType
                           let k = tmpLocaType.FirstOrDefault()

                           join d in Entities.DevInfo_Tab on a.DeviceId equals d.DeviceId
                            into tmpDevInfo
                           let DevInfo = tmpDevInfo.FirstOrDefault()
                           select new LocaInfoExtendModel
                           {
                               LocaId = a.LocaId,
                               LocaName = LocaInfoTab.LocaName,
                               LocaTypeId = LocaInfoTab.LocaTypeId,
                               LocaTypeName = k == null ? "" : k.LocaTyp,
                               LocaHigh = LocaInfoTab.LocaHigh,
                               Describe = LocaInfoTab.Describe,
                               Link = LocaInfoTab.Link,
                               PowerMode = LocaInfoTab.PowerMode,
                               District = LocaInfoTab.District,
                               Town = LocaInfoTab.Town,
                               Longitude = LocaInfoTab.Longitude,
                               Latitude = LocaInfoTab.Latitude,
                               Altitude = LocaInfoTab.Altitude,
                               NaviLongitude = LocaInfoTab.NaviLongitude,
                               NaviLatitude = LocaInfoTab.NaviLatitude,
                               StopLongitude = LocaInfoTab.StopLongitude,
                               StopLatitude = LocaInfoTab.StopLatitude,
                               Village = LocaInfoTab.Village,
                               Remark = a.Remark,
                               AddTime = a.AddTime,
                               Code = a.Code,
                               CodeId = a.CodeId,
                               DeviceId=a.DeviceId,
                               DeviceLevel = DevInfo.DeviceLevel
                           };
                //模糊查询
                if (!string.IsNullOrEmpty(request.Indistinct))
                {
                    linq = linq.Where(i => i.LocaName.Contains(request.Indistinct) || i.District.Contains(request.Indistinct) || i.Town.Contains(request.Indistinct));
                }
                //排除子设备信息
                linq = linq.Where(i => i.DeviceLevel == 1);
                //通过项目编号查询
                if (request.Type == 1)
                {
                    linq = linq.Where(i => i.Code == request.LPDId && i.CodeId == "1");
                }
                //通过设备编号查询
                if (request.Type == 3)
                {
                    linq = linq.Where(i => i.DeviceId == request.LPDId);
                }
                list.TotalCount = linq.Count();
                List<LocaInfoExtendModel> LocaInfolist = linq.OrderByDescending(i => i.AddTime).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();

                list.List = LocaInfolist;

                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;
            }


            return list;
        }
    }
}
